<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class BaseController extends Controller
{
    //分页的每页显示条数
    protected $pageSize = 10;

    /**
     * 返回数据
     * @param $error_code 错误码
     * @param $data       返回的数据
     * @param $msg        提示消息
     * @param $code       http状态码
     * @return void
     */
    public function writeJson($error_code = 0, $data = [], $msg = 'success', $code = 200)
    {
        return response()->json([
            'error_code' => $error_code,
            'data' => $data,
            'msg' => $msg
        ], $code);
    }


    /**
     * 递归处理层级
     * @return void
     */
    public function sonLevel($data, $pid = 0)
    {
        $arr = [];
        foreach ($data as $key => $val) {
            //等于0的时候代表是顶级菜单
            if ($val['pid'] == $pid) {
                //递归处理子集
                $val['son'] = $this->sonLevel($data, $val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }
}
